/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.system;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author ciummo
 */
@Stateless
public class UtenteFacade extends AbstractFacade<Utente> implements UtenteFacadeRemote {
    @PersistenceContext(unitName = "DGestEJBSeverPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UtenteFacade() {
        super(Utente.class);
    }
    
    public boolean checkUserName(String username) {
        try {
            String sql = "SELECT u FROM Utente u WHERE u.username = :username";
            Query query = em.createQuery(sql);
            query.setParameter("usrnm", username);
            if (query.getResultList().isEmpty()) {
                return true;
            }
            return false;

        } catch (Exception ex) {
            return false;
        }
    }
    
    // TODO : CANCELLARE
    public Utente findUtente (String username){
        
        Utente u = null;
        
        try {
            String sql = "SELECT u FROM Utente u WHERE u.username = :username";
            Query query = em.createQuery(sql);
            query.setParameter("username", username);
            
            u = ((Utente) query.getResultList().get(0));
            }
            catch (Exception ex) {
                return u;
            }
        return u;
    }
        
    
    
    
    
    
}
